package controllers

import (
	"ginstudydemo.com/main/pkg/logger"
	"ginstudydemo.com/main/service"
	"github.com/gin-gonic/gin"
)

// 一级分类项
type Result struct {
	// 一级分类id
	ID string `json:"id"`
	// 一级分类图片集
	ImageBanners []string `json:"imageBanners"`
	// 一级分类名称
	Name string `json:"name"`
	// 一级分类图片
	Picture string `json:"picture"`
}
type IBookController interface {
	GetInfo()
	GetList()
	Add()
}

type BookController struct{}

func NewBookController() *BookController {
	return &BookController{}
}

func (u BookController) GetInfo(c *gin.Context) {

	logger.Write("日志信息", "book")
	//defer func() {
	//	err := recover()
	//	if err != nil {
	//		service.ReturnErr(c, "9999", err)
	//	}
	//}()

	//错误测试代码
	//num1 := 23
	//num2 := 0
	//str3 := strconv.Itoa(num1 / num2)
	//fmt.Println(str3)
	id := c.Param("id")
	service.ReturnErr(c, "9999", "error:系统错误#id:"+id)
}

func (u BookController) GetList(c *gin.Context) {
	service.ReturnSuccess[[]Result](c, "2000", "Book：ok", []Result{{
		ID:           "123",
		ImageBanners: []string{"image3.jpg", "image4.jpg"},
		Name:         "Categor22",
		Picture:      "picture2.jpg",
	}})
}

type Search struct {
	Name string `json:"name"`
	Cid  int    `json:"cid"`
}

func (u BookController) Add(c *gin.Context) {

	//使用map
	//param := make(map[string]interface{})
	//err := c.BindJSON(&param)

	//使用结构体
	param := &Search{}
	err := c.BindJSON(param)

	if !(err == nil) {
		service.ReturnErr(c, "9999", "error:系统错误#")
	}

	service.ReturnSuccess[Search](c, "2000", "Book：ok", *param)
}
